package com.example.rwq_test.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 找出数组中和为目标值的所有组合，可重复
 *
 * @Author：dingdang4D
 * @Date：2024/4/24
 */
public class CombinationSum {
    private static List<List<Integer>> result = new ArrayList<>();
    private static List<Integer> tempList = new ArrayList<>();

    /**
     * 忽略顺序
     *
     * @param candidates
     * @param target
     * @param start
     */
    private static void combinationSum(int[] candidates, int target, int start) {
        if (target < 0) {
            return;
        } else if (target == 0) {
            result.add(new ArrayList<>(tempList));
        } else {
            for (int i = start; i < candidates.length; i++) {
                tempList.add(candidates[i]);
                combinationSum(candidates, target - candidates[i], i);
                tempList.remove(tempList.size() - 1);
            }
        }
    }

    /**
     * 不忽略顺序
     *
     * @param candidates
     * @param target
     * @param start
     */
    private static void combinationSum1(int[] candidates, int target, int start) {
        if (target < 0) {
            return;
        } else if (target == 0) {
            result.add(new ArrayList<>(tempList));
        } else {
            for (int i = start; i < candidates.length; i++) {
                tempList.add(candidates[i]);
                combinationSum1(candidates, target - candidates[i], start);
                tempList.remove(tempList.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        int[] candidates = {2, 3, 6, 7};
        int target = 7;
        combinationSum1(candidates, target, 0);
        System.out.println(result);
    }
}
